
import { loginByUsername, refreshToken } from '@/apis/login';
const user = {
  state: {
    user: '',
    token: sessionStorage.getItem('Authorization')
  },
  mutations: {
    SET_USER: (state, user) => {
      state.user = user;
    },
    SET_TOKEN: (state, token) => {
      state.token = token;
    }
  },
  actions: {
    loginByUsername ({commit}, loginForm) {
      return new Promise((resolve, reject) => {
        loginByUsername(loginForm.username, loginForm.password).then(response => {
          const obj = response.data;
          if (obj.success) {
            let tokenObj = obj.obj.tokenObj;
            let userObj = obj.obj.user;
            let token = tokenObj.tokenType + ' ' + tokenObj.accessToken;
            sessionStorage.setItem('Authorization', token);
            sessionStorage.setItem('user', JSON.stringify(userObj));
            commit('SET_TOKEN', token);
            commit('SET_USER', userObj);
          }
          resolve(response);
        }).catch(error => {
          reject(error);
        });
      });
    },
    refreshToken ({commit}) {
      return new Promise((resolve, reject) => {
        refreshToken().then(response => {
          const obj = response.data;
          if (obj.success) {
            let token = response.data.obj.tokenType + ' ' + response.data.obj.accessToken;
            sessionStorage.setItem('Authorization', token);
            commit('SET_TOKEN', token);
          }
          resolve();
        }).catch(error => {
          reject(error);
        });
      });
    },
    // 前端 登出
    FedLogOut ({commit}) {
      return new Promise(resolve => {
        commit('SET_TOKEN', '');
        commit('SET_USER', '');
        sessionStorage.clear();
        resolve();
      });
    }
  }
};

export default user;
